package com.slt.android.osmessageandsecurity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 1.要获得系统配置信息、通常可以从以下两个方面获取
 * android.os.Build
 * SystemProperty
 * 储存系统信息的目录/proc
 */
public class MainActivity extends Activity implements View.OnClickListener, View.OnLongClickListener {
    PackageManager pm;

    Button btn1, btn2 , btn3 , btnIntent;
    TextView tv;
    private final int ALL_APP = 100;
    private final int SYSTEM_APP = 101;
    private final int THRID_APP = 102;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        try {
            usePackgeManager();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btnIntent = (Button) findViewById(R.id.btnIntent);
        tv = (TextView) findViewById(R.id.tv);


        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btnIntent.setOnClickListener(this);
        btn3.setOnLongClickListener(this);
    }

    /**
     * 上面说的第1项-->主要是用于获取系统信息的
     * 2.使用packageManager查看APK应用信息
     *
     * 最里面的信息代表整个activity的信息、系统提供了ActivityInfo来进行封装
     * 最外面的代表整个MainIfest文件中节点的信息、系统提供了PackageInfo来进行封装
     * Android系统提供了PackageManager来负责已安装的app
     *
     * ActivityInfo封装了在MainIfest中<activity></activity>和<recevier></recevier>中的所有信息，包括name、icom、lable、launchMode
     *
     * ServiceInfo与activityInfo类似，它封装了<service></service>之间的所有信息
     *
     * ApplicationInfo、它封装了<Application></Application>之间的信息，不过特别的是，Application包含很多Flag
     * FLAG_SYSTEM表示为系统应用、FLAG_EXTERNAL_STORAGE表示安装在SDCard上的应用，通过这些flag可以很方便的判断应用的类型
     *
     * PackageInfo与前面三个info类似，都是用于封装MainIfest文件的相关节点信息、而PackageInfo包含了所有的Activity、service等信息
     *
     * ResolveInfo比较特殊、它封装的是包含<intent>信息的上一级信息，所以它可以返回ActivityInfo\ServiceInfo等包含<intent>、
     * 它经常用来帮助我们找到那些特定的intent条件信息、如带分享功能、播放功能的应用
     *
     * -----------------------------------------------------------------------------------------------------------------
     * 有了上面这些用于封装的Bean对象之后、PackageManager就可以通过调用各种方法、返回不同的Bean对象了、
     */
    private void usePackgeManager() throws PackageManager.NameNotFoundException {
        //getPackageManager()
        pm = getPackageManager();
        //
        //getApplicationInfo;
        ApplicationInfo thisApplicationInfo = pm.getApplicationInfo("com.slt.android.osmessageandsecurity" , 0);
        Log.e("" , "");
        getAppInfo(0);
    }

    private void getAppInfo(int flag) {

        //获取应用信息
        List<ApplicationInfo> listApplication = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);

        List<PMAppInfo> appInfos = new ArrayList<>();

        //判断应用类型
        switch (flag){
            case ALL_APP:
                for (ApplicationInfo app : listApplication) {
                    appInfos.add(makeAppInfo(app));
                }
                break;
            case SYSTEM_APP:
                for (ApplicationInfo app : listApplication) {
                    if((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ){
                        appInfos.add(makeAppInfo(app));
                    }
                }
                break;
            case THRID_APP:
                for (ApplicationInfo app : listApplication) {
                    if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0){
                        appInfos.add(makeAppInfo(app));
                    }else if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
                        appInfos.add(makeAppInfo(app));
                    }
                }
                break;
        }

        if(appInfos != null){
            String str = "";
            for (PMAppInfo appInfo : appInfos) {
                str = str + "包名：" + appInfo.getPackageName() + "  应用名：" + appInfo.getAppLable() + "\n";
            }
            tv.setText(str);
        }

    }

    private PMAppInfo makeAppInfo(ApplicationInfo app) {
        PMAppInfo appInfo = new PMAppInfo();
        appInfo.setAppIcon(app.loadIcon(pm));
        appInfo.setAppLable((String) app.loadLabel(pm));
        appInfo.setPackageName(app.packageName);
        return appInfo;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                getAppInfo(ALL_APP);
                break;
            case R.id.btn2:
                getAppInfo(SYSTEM_APP);
                break;
            case R.id.btn3:
                getAppInfo(THRID_APP);
            case R.id.btnIntent:
                //resolveActivity方法
                Intent i = new Intent(MainActivity.this , SeceondActiviy.class);
                i.putExtra("i" , "i");
                startActivity(i);
                break;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        //获取应用信息
        List<ApplicationInfo> listApplication = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);

        List<PMAppInfo> appInfos = new ArrayList<>();

        for (ApplicationInfo app : listApplication) {
            if((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0 ){
                appInfos.add(makeAppInfo(app));
            }
        }
        if(appInfos != null){
            String str = "";
            for (PMAppInfo appInfo : appInfos) {
                str = str + "包名：" + appInfo.getPackageName() + "  应用名：" + appInfo.getAppLable() + "\n";
            }
            tv.setText(str);
        }

        return true;
    }
}
